<?php

namespace App\Http\Controllers\Home;

use App\Model\Branch;
use App\Model\BranchTest;
use App\Model\Order;
use App\Model\OrderList;
use App\Model\PushOrder;
use App\Model\tel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Session;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\OrderExport;
use App\Models\Order as OrderModel;

class ExcelController extends Controller
{
    //Excel文件导出功能order表
    public function export(Request $request){
        $orders = Order::OrderBy("id","desc")->where(function($query) use($request){
            $data = $request->all();
            if(isset($data['start'])){
                $query->where('buytime','>',strtotime($data['start']));
            }
            if(isset($data['end'])){
                $query->where('buytime','<',strtotime($data['end']));
            }
        })->get();
        //设置表头
        $row = [[
            "bid"=>'下游id',
            "chargeaccount"=>'充值账号',
            "status"=>'订单状态',
            "branch"=>'充值状态',
            "buytime"=>'购买时间',
            "num"=>'购买数量',
            "productprice"=>'购买价格',
            "productname"=>'订单成本',
            "remainingnumber"=>'订单价格',
            "orderno"=>'订单号',
            "oktime"=>'完成时间'
        ]];
        $list = [];
        $index = 0;
        foreach ($orders as $order){
            $data = Branch::where('id',$order->bid)->select("username")->first();
            $branch = $data['username'];
            $list[$index]['bid'] = $branch;
            $list[$index]['chargeaccount'] = $order->chargeaccount."\t";
            $list[$index]['status'] = $order->status;
            $list[$index]['branch'] = $order->branch;
            $list[$index]['buytime'] = date("Y-m-d H:i:s",$order->buytime);
            $list[$index]['num'] = $order->num;
            preg_match('/\d+/',$order->productname,$price);
            $list[$index]['productprice'] = $price[0];
            $list[$index]['productname'] = $order->productprice*$order->num;
            $list[$index]['remainingnumber'] = $order->remainingnumber*$order->num;
            $list[$index]['orderno'] = $order->orderno."\t";
            $list[$index]['oktime'] = $order->oktime."\t";
            $index++;
        }
        $data1['filename'] = date('Y-m-d H:i:s') . '订单列表.xls';
        $user = $request->session()->get('user');
        if(isset($user)){
            $data1['username'] = $user['username'];
        }
        OrderList::create($data1);
        //执行导出
        return Excel::download(new OrderExport($row,$list), $data1['filename']);
    }

    //Excel文件导出功能pushorder表
    public function exportOrder(Request $request){
        $orders = PushOrder::OrderBy("id","desc")->where(function($query) use($request){
            $data = $request->all();
            if(isset($data['start'])){
                $query->where('buytime','>',strtotime($data['start']));
            }
            if(isset($data['end'])){
                $query->where('buytime','<',strtotime($data['end']));
            }
        })->get();
        //设置表头
        $row = [[
            "bid"=>'下游id',
            "chargeaccount"=>'充值账号',
            "status"=>'订单状态',
            "branch"=>'充值状态',
            "buytime"=>'购买时间',
            "num"=>'购买数量',
            "productprice"=>'购买价格',
            "productname"=>'订单成本',
            "remainingnumber"=>'订单价格',
            "orderno"=>'订单号',
            "oktime"=>'完成时间'
        ]];
        $list = [];
        $index = 0;
        foreach ($orders as $order){
            if($order->bid == 8){
                $branch = "放羊娃";
            }elseif($order->bid == 9){
                $branch = "光总";
            }elseif($order->bid == 10){
                $branch = "华通";
            }elseif($order->bid == 12){
                $branch = "软游";
            }elseif($order->bid == 888){
                $branch = "福禄";
            }else{
                $branch = "无下线";
            }
            $list[$index]['bid'] = $branch;
            $list[$index]['chargeaccount'] = $order->chargeaccount."\t";
            $list[$index]['status'] = $order->status;
            $list[$index]['branch'] = $order->branch;
            $list[$index]['buytime'] = date("Y-m-d H:i:s",$order->buytime);
            $list[$index]['num'] = $order->num;
            preg_match('/\d+/',$order->productname,$price);
            $list[$index]['productprice'] = $price[0];
            $list[$index]['productname'] = $order->productprice;
            $list[$index]['remainingnumber'] = $order->remainingnumber;
            $list[$index]['orderno'] = $order->orderno."\t";
            $list[$index]['oktime'] = $order->oktime."\t";
            $index++;
        }
        $data1['filename'] = date('Y-m-d H:i:s') . '拆单列表.xls';
        $user = $request->session()->get('user');
        if(isset($user)){
            $data1['username'] = $user['username'];
        }
        OrderList::create($data1);
        //执行导出
        return Excel::download(new OrderExport($row,$list), $data1['filename']);
    }

    //Excel文件导出功能order表
    public function exportHf(Request $request){
        $orders = tel::OrderBy("id","desc")->where(function($query) use($request){
            $data = $request->all();
            if(isset($data['start'])){
                $query->where('buytime','>',strtotime($data['start']));
            }
            if(isset($data['end'])){
                $query->where('buytime','<',strtotime($data['end']));
            }
        })->get();
        //设置表头
        $row = [[
            "bid"=>'上游id',
            "pid"=>'下游id',
            "chargeaccount"=>'充值账号',
            "status"=>'订单状态',
            "branch"=>'充值状态',
            "buytime"=>'购买时间',
            "price"=>'购买价格',
            "orderno"=>'订单号',
            "order"=>'外部订单号',
            "oktime"=>'完成时间'
        ]];
        $list = [];
        $index = 0;
        foreach ($orders as $order){
            $data = BranchTest::where('id',$order->bid)->select("username")->first();
            $data1 = BranchTest::where('id',$order->pid)->select("username")->first();
            $list[$index]['bid'] = $data['username'];
            $list[$index]['pid'] = $data1['username'];
            $list[$index]['chargeaccount'] = $order->chargeaccount."\t";
            $list[$index]['status'] = $order->status;
            $list[$index]['branch'] = $order->branch;
            $list[$index]['buytime'] = date("Y-m-d H:i:s",$order->buytime);
            $list[$index]['price'] = $order->price;
            $list[$index]['orderno'] = $order->orderno."\t";
            $list[$index]['order'] = $order->orderno."\t";
            $list[$index]['oktime'] = $order->oktime."\t";
            $index++;
        }
        $data2['filename'] = date('Y-m-d H:i:s') . '话费列表.xls';
        $user = $request->session()->get('user');
        if(isset($user)){
            $data2['username'] = $user['username'];
        }
        print_r($data2);
        OrderList::create($data2);
        //执行导出
        return Excel::download(new OrderExport($row,$list), $data2['filename']);
    }

}
